import $service from '../../../../service.js';
var hospital_id="1";
var that;
var log_id;
var doctor_id;
Page({
    data:{
       active : true,
       inquiryMap:'',
       prescriptionList:[]
    },
    onLoad(e){
        that = this.data;
        log_id = e.log_id
    },
    onShow(){
        that = this.data;
        doctor_id = $service.getUserLocalStorage().doctor_id;
        this.publicUtil_getInquiryLogDetails()
    },
    //数据
    publicUtil_getInquiryLogDetails:function(){
        var jsonData ={
            "is_receive":"1",
            "log_id" : log_id
        }
        var jsonAjax = {
            "url": "publicUtil_getInquiryLogDetails.do",
            "jsonData": jsonData,
        }
        $service.getAjaxData(jsonAjax, this.publicUtil_getInquiryLogDetails_back);
    },
    //回调
    publicUtil_getInquiryLogDetails_back: function(jsonBack){
         var data=$service.strToJson(jsonBack.data).data;
         var prescriptionList = data.prescriptionList; 
         var inquiryMap= data.inquiryMap; //患者信息
         var doctorMap = data.doctorMap; //医生信息
         inquiryMap.lrsj2 = $service.nothinglrsj(inquiryMap.lrsj)
         
         if (!$service.isUndefinedAndEmpty(inquiryMap.lof_diagnosis)) {
             var lof_diagnosis = '';
             var lof_diagnosis2 = '';
             var numC =0
             var lofdiagnosislist = inquiryMap.lof_diagnosis.split(",")
             for (var i = 0; i < lofdiagnosislist.length; i++) {
                 var index = lofdiagnosislist[i].indexOf('自定义诊断');
                 if (index > -1) {
                     lof_diagnosis2 += lofdiagnosislist[i] + ",";
                 } else {
                     numC++
                     lof_diagnosis += "(" + numC + ")" + lofdiagnosislist[i] + ",";
                 }
             }
             inquiryMap.diagnosisMap = lof_diagnosis + lof_diagnosis2;
             inquiryMap.diagnosisMap = inquiryMap.diagnosisMap.substring(0, inquiryMap.diagnosisMap.length - 1);
         } else {
             inquiryMap.diagnosisMap =''
         }
         this.setData({
             inquiryMap : inquiryMap,
             prescriptionList: prescriptionList
         })
    },
    gotodetalis(e){
        var index = e.currentTarget.dataset.index;
        if(index == 0 || index == 1 || index == 2 || index == 3 ||index == 4 ){//0过敏史 1 主诉 2现病史 3既往史 4诊断
            $service.openPage('../index_diagnosis/index_diagnosis?inquiryMap=' + $service.jsonToStr(that.inquiryMap) +'&index='+ index)
        }else if(index == 5){
            var num = e.currentTarget.dataset.num;
            $service.openPage('../../../user_side/index/index_prescription_details/index_prescription_details?prescription_id=' + this.data.prescriptionList[num].prescription_id)
        }else if(index == 6 || index == 7){//中西药开方
            if($service.isUndefinedAndEmpty(that.inquiryMap.log_main_suit)){
                $service.showToast("请先输入主诉")
                return;
            }
            if($service.isUndefinedAndEmpty(that.inquiryMap.diagnosisMap) ){
                $service.showToast("请先输入诊断")
                return;
            }
            if(index == 6){
                 var drug_type = "2"
            }else{
                 var drug_type = "1"
            }
            var obj={
                "inquiry_patient_id" : this.data.inquiryMap.inquiry_patient_id,
                "drug_type" : drug_type,
                "log_id" : log_id
                
            }
            $service.openPage('index_choose_drugs/index_choose_drugs?obj=' + $service.jsonToStr(obj))
        }
    },
    //删除处方
    deletImg(e){
        var this_ = this
        var index = e.currentTarget.dataset.index;
        wx.showModal({
            title: '温馨提示',
            content: '确定要将此处方作废？',
            success(res) {
                if (res.confirm) {
                    var jsonData ={
                        "prescription_id" : that.prescriptionList[index].prescription_id
                    }
                    var jsonAjax = {
                        "url": "doctorSide_personal_prescription_deletePrescription.do",
                        "jsonData": jsonData,
                    }
                    $service.getAjaxData(jsonAjax,function(jsonBack){
                        var selToID=''
                        if(that.inquiryMap.join_type == 1){
                             selToID = "U" + that.inquiryMap.join_id
                        }else{
                              selToID = "S" + that.inquiryMap.join_id
                        }
                         var sendSystemMessageMsgText = ''
                        if(that.prescriptionList[index].prescription_type == 1){
                            var sendSystemMessageMsgText = '医生已撤销中药处方'
                        }else if(that.prescriptionList[index].prescription_type == 2){
                            var sendSystemMessageMsgText = '医生已撤销西药处方'
                        }
                        wx.txTic.sendCustomMessage(selToID,6)
                        wx.txTic.sendSystemMessageMsg(selToID,sendSystemMessageMsgText);
                        $service.showToast('处方删除成功')
                         this_.publicUtil_getInquiryLogDetails()
                    });
                }
            }
        }) 
    },
})